package no.ntnu.item.ttm4115.termproject.configurationagent;

import java.util.Random;

import com.thoughtworks.xstream.XStream;

import no.tellu.common.javaframe.ActorAddress;
import no.tellu.common.javaframe.messages.RoleRequestMsg;
import no.tellu.common.actorframe.messages.RoleDeniedMsg;
import no.tellu.common.actorframe.messages.RoleConfirmMsg;
import no.ntnu.item.ttm4115.termproject.RegisterTeacherAgent;
import no.ntnu.item.ttm4115.termproject.FetchConfiguration;
import no.ntnu.item.ttm4115.termproject.CreateNewGameFromGUI;
import no.ntnu.item.ttm4115.termproject.StartGameFromGUI;
import no.ntnu.item.ttm4115.termproject.StartGame;
import no.ntnu.item.ttm4115.termproject.extrasagent.Game;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionOption;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionQuestion;
import no.ntnu.item.ttm4115.termproject.extrasagent.Treasures;
import no.ntnu.item.ttm4115.termproject.SendGMAddressToTeacher;
import no.ntnu.item.ttm4115.termproject.RegisterTA;
import no.ntnu.item.ttm4115.termproject.SendTreasureToConfigAgent;
import no.ntnu.item.ttm4115.termproject.UpdateTreasureInXML;
import no.ntnu.item.ttm4115.termproject.DeleteTreasureFromGUI;
import no.ntnu.item.ttm4115.termproject.UpdateGroupFromGUI;


public class ConfigurationAgentActions extends Object {

	public static void fetchConfig(RegisterTeacherAgent signal, ConfigurationAgentSM asm){
		asm.teacherAddress = signal.getSenderRole();
		int uniqueId = new Random().nextInt();
		asm.sendMessage(new RoleRequestMsg(""+uniqueId,"ConfigurationAgentGM"), asm.getMyActorAddress());
	}
	
	public static void deniedConfig(RoleDeniedMsg signal, ConfigurationAgentSM asm){
	    System.out.println("RoleRequest Denied by the ConfigurationAgentGM!!!!");
	}
	
	public static void getConfig(RoleConfirmMsg signal, ConfigurationAgentSM asm){
	    asm.sendMessage(new FetchConfiguration(asm.teacherAddress,asm.games), signal.getSenderRole());
	}
	@SuppressWarnings("unchecked")
	public static void writeOutNewGame(CreateNewGameFromGUI signal, ConfigurationAgentSM asm){
		XStream xstream = new XStream();
		xstream.setClassLoader(new Game().getClass().getClassLoader());
		xstream.alias("game", Game.class);
		xstream.alias("treasures", Treasures.class);
		xstream.alias("group",Group.class);
		xstream.alias("player",Player.class);
		xstream.alias("sessionoption",SessionOption.class);
		xstream.alias("sessionquestion", SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlGame);
		Game game = (Game)object;
		asm.games.put(game.getName(), game);
		asm.gmLoadConfig.writeGameToXMLFile(game);
	}

	public static void fetchAndStartGame(StartGameFromGUI signal, ConfigurationAgentSM asm){
		asm.newGame = (Game) asm.games.get(signal.gameName);
		int uniqueId = (int) ((Math.random()*1000000)+1);
		asm.sendMessage(new RoleRequestMsg(""+uniqueId,"GameManager"), asm.getMyActorAddress());
	}

	public static void roleDenied(RoleDeniedMsg signal, ConfigurationAgentSM asm){
	    System.out.println("Role Denied received by the ConfigurationAgent from the GameManager");
	}

	public static void startGame(RoleConfirmMsg signal, ConfigurationAgentSM asm){
		 XStream xstream = new XStream();
			xstream.setClassLoader(new Game().getClass().getClassLoader());
			xstream.alias("game", Game.class);
			xstream.alias("treasures", Treasures.class);
			xstream.alias("group",Group.class);
			xstream.alias("player",Player.class);
			xstream.alias("sessionoption",SessionOption.class);
			xstream.alias("sessionquestion", SessionQuestion.class);
			xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		asm.sendMessage(new StartGame(asm.newGame), signal.getSenderRole());
	   
	        
	    asm.sendMessage(new SendGMAddressToTeacher(asm.newGame.getName(),signal.getSenderRole()), asm.teacherAddress);

	}

	public static void writeOutNewTreasure(SendTreasureToConfigAgent signal, ConfigurationAgentSM asm){
	    XStream xstream = new XStream();
	    xstream.setClassLoader(new Treasures().getClass().getClassLoader());
		xstream.alias("treasures", Treasures.class);
		xstream.alias("sessionoption", SessionOption.class);
		xstream.alias("sessionquestion",SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlTreasure);
		Treasures treasures = (Treasures)object;
		asm.gmLoadConfig.getTreasures().add(treasures);
		asm.gmLoadConfig.writeTreasureToXMLFile(treasures);
	}

	public static void writeAndUpdate(UpdateTreasureInXML signal, ConfigurationAgentSM asm){
		XStream xstream = new XStream();
	    xstream.setClassLoader(new Treasures().getClass().getClassLoader());
		xstream.alias("treasures", Treasures.class);
		xstream.alias("sessionoption", SessionOption.class);
		xstream.alias("sessionquestion",SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlTreasure);
		Treasures treasure = (Treasures)object;
		asm.gmLoadConfig.updateTreasureHash(treasure,signal.oldName);
	}

	public static void deleteTreasure(DeleteTreasureFromGUI signal, ConfigurationAgentSM asm){
		XStream xstream = new XStream();
	    xstream.setClassLoader(new Treasures().getClass().getClassLoader());
		xstream.alias("treasures", Treasures.class);
		xstream.alias("sessionoption", SessionOption.class);
		xstream.alias("sessionquestion",SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlTreasure);
		Treasures treasure = (Treasures)object;
		asm.gmLoadConfig.deleteTreasureInXMLFile(treasure.getName());
	}
	



}
